package com.xyw.his00.utils.FileUtils;

import com.alibaba.fastjson.annotation.JSONField;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.xyw.his00.dto.InvoiceDTO;
import com.xyw.his00.pojo.dbpojo.*;
import com.xyw.his00.utils.TimeUtils.DateUtil;
import com.xyw.his00.utils.TimeUtils.LocalDateTimeRevertStringUtil;
import com.xyw.his00.vo.PatientRequest;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
@Component
public class PayPdfUtil {
        public static void ExportPdf(HttpServletRequest request,
                                     HttpServletResponse response,
                                    @JSONField InvoiceDTO invoiceDTO) throws Exception {
                //告诉浏览器用什么软件可以打开此文件
                //content-Type: application/pdf?还有什么类型
                response.setHeader("content-Type", "application/pdf");
                //下载文件的默认名称
                // response.setHeader("Content-Disposition", "attachment;filename=XXX.pdf");
                //设置中文
                BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
                Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);//设置中文字体,字号12，正常格式
                //创建一个文档
                Document document = new Document(PageSize.A4);
                //创建第一个段落
                Paragraph titleParagraph = new Paragraph();
                //支持中文
                titleParagraph.setFont(new Font(bfChinese, 20, Font.NORMAL));
                //设置居中显示
                titleParagraph.setAlignment(Element.ALIGN_CENTER);
                titleParagraph.add("医院门诊病情消费记录单");
                //创建第二个段落
                Paragraph tipsParagraph = new Paragraph();
                tipsParagraph.setFont(new Font(bfChinese, 10, Font.NORMAL));
                tipsParagraph.setAlignment(Element.ALIGN_CENTER);
                tipsParagraph.setLeading(tipsParagraph.getTotalLeading() + 10);
                tipsParagraph.add("打印时间：" + DateUtil.getTodayYmd());

                PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
          List<PayOrder> list=invoiceDTO.revertToPayOrder();




                // 打开文档
                document.open();
                //设置文档标题
                document.addTitle("门诊病情诊断记录单");
                //设置文档作者
                document.addAuthor("跟阎王爷抢人重写生死簿爱国为民专业事务所");
                document.addCreationDate();
                //设置关键字
                document.addKeywords("iText");
                document.addLanguage("中文");
                //增加段落进入文档
                document.add(titleParagraph);
                document.add(tipsParagraph);
                //表格
                PdfPTable tableMessage = new PdfPTable(4);
                tableMessage.setSpacingBefore(8f);
                tableMessage.setSpacingAfter(8f);
                //设置表格无边框
                tableMessage.getDefaultCell().setBorder(0);
                //设置表格宽度
                tableMessage.setTotalWidth(new float[]{30, 120, 30, 120});
                tableMessage.addCell(new Paragraph("患者姓名及编号", FontChinese));
                tableMessage.addCell(new Paragraph(invoiceDTO.getRegisterName()+invoiceDTO.getRegisterId(), FontChinese));

                tableMessage.addCell(new Paragraph("订单编号", FontChinese));
                tableMessage.addCell(String.valueOf(invoiceDTO.getOutTradeNo()));
                tableMessage.addCell(new Paragraph("病历号", FontChinese));
                tableMessage.addCell(invoiceDTO.getOrderNo());
                document.add(tableMessage);


                //病情表格
                PdfPTable tableOrder = new PdfPTable(1);
                //设置表格无边框
                tableOrder.getDefaultCell().setBorder(0);
                tableOrder.setSpacingBefore(30f);
                tableOrder.setSpacingAfter(10f);
                PdfPCell cell = new PdfPCell(new Paragraph("患者检查", new Font(bfChinese, 14, Font.NORMAL)));
                cell.setFixedHeight(25);

                PdfPCell cell2=new PdfPCell();
                PdfPCell cell3=new PdfPCell();
                PdfPCell cell4=new PdfPCell();
                PdfPCell cell5=new PdfPCell();
                PdfPCell cell6=new PdfPCell();

                com.itextpdf.text.pdf.PdfPCell cell1=new PdfPCell();
            cell1 = new PdfPCell(new Paragraph("总价格 ", new Font(bfChinese, 14, Font.NORMAL)));
            cell1.setFixedHeight(25);
            cell1.setBorder(0);
            cell2 = new PdfPCell(new Paragraph(String.valueOf(invoiceDTO.getTotalAmount()), new Font(bfChinese, 14, Font.NORMAL)));
            cell2.setFixedHeight(25);
            cell2.setBorder(0);
            for (PatientRequest  patientRequest:invoiceDTO.getPatientRequests()) {

          cell3 = new PdfPCell(new Paragraph("检查项目及单价 ", new Font(bfChinese, 14, Font.NORMAL)));
          cell3.setFixedHeight(25);
          cell3.setBorder(0);
          cell4 = new PdfPCell(new Paragraph(patientRequest.getTechName()+patientRequest.getTechPrice(), new Font(bfChinese, 10, Font.NORMAL)));
                        cell4.setFixedHeight(30);
                        cell4.setBorder(0);
                        cell4.setPaddingLeft(10);

                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 选择你需要的格式
                    LocalDateTime createTime = patientRequest.getCreateTime();
                    String createTimeString = createTime.format(formatter);
                  cell5 = new PdfPCell(new Paragraph("检查创建时间", new Font(bfChinese, 14, Font.NORMAL)));
                        cell5.setFixedHeight(25);
          cell6 = new PdfPCell(new Paragraph(createTimeString, new Font(bfChinese, 10, Font.NORMAL)));
                        cell6.setFixedHeight(30);
                        cell6.setBorder(0);
                        cell6.setPaddingLeft(10);

                }



                tableOrder.addCell(cell1);
                tableOrder.addCell(cell2);
                tableOrder.addCell(cell3);
                tableOrder.addCell(cell4);
                tableOrder.addCell(cell5);
                tableOrder.addCell(cell6);
                document.add(tableOrder);


                PdfContentByte cb = writer.getDirectContent();
                BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
                cb.beginText();
                cb.setFontAndSize(bf, 11);
                cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "该报告单仅供参考", 300, 40, 0);
                cb.setFontAndSize(bf, 13);
                cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "宇宙声明仅归医生所有", 300, 20, 0);
                cb.endText();
                document.close();

        }
}